
package com.idealighter.game.roomcard.common;

import com.idealighter.game.core.assertions.HuohuaAssert;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.roomcard.dto.RoomCardMemInfoDto;
import com.idealighter.game.roomcard.struct.RoomCardRoomCfg;
import com.idealighter.game.roomcard.struct.RoomCardRoomCfgValue;
import com.idealighter.game.roomcard.struct.convert.RoomCardMemInfoDtoConvert;
import com.idealighter.game.server.context.ApplicationContext;
import com.idealighter.utils.check.EmptyUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 游戏房间 .
 * 
 * 
 * @date 2015年8月6日 下午6:08:31
 *
 */
public abstract class AbstractRoomCardRoomWithTable<//
    T extends AbstractRoomCardTable<S, RCFGT, RCFGVT>, //
    S extends AbstractRoomCardSeat<RCFGT, RCFGVT>, //
    RCFGT extends RoomCardRoomCfg<RCFGVT>, //
    RCFGVT extends RoomCardRoomCfgValue> extends AbstractRoomCardRoom {
  private static final Logger LOG = LoggerFactory.getLogger(AbstractRoomCardRoomWithTable.class);

  // 房间包含的桌子
  private final T table;
  @Getter
  private final RCFGT roomCfg; // 房间配置

  /**
   * 构造函数.
   * 
   * @param roomId 房间编号.
   * @param roomName 房间名称.
   * @param ownerId 房主编号.
   * @param groupId 群编号(0:不属于任何群).
   * @param maxSeatNum 桌子数量.
   * @param roomCfg 房卡配置.
   */
  public AbstractRoomCardRoomWithTable(long roomId, String roomName, long ownerId, int groupId,
      int maxSeatNum, // int maxByStanderNum,
      RCFGT roomCfg) {
    super(roomId, roomName, ownerId, groupId, maxSeatNum);
    // 房间配置(注意: 需要放置在createTalbe之前)
    this.roomCfg = roomCfg;
    T table = createTable(this);
    if (table == null) {
      LOG.error("房间[{}]创建桌子失败，桌子为空", id);
      HuohuaAssert.isTrue(false);
    }
    this.table = table;
  }

  /**
   * @Title createTable.
   * @Description 创建桌子
   * @date 2018年5月10日 下午5:54:07
   * @param room 房间
   */
  public abstract T createTable(AbstractRoomCardRoomWithTable<T, S, RCFGT, RCFGVT> room);

  /**
   * 玩家房间成员信息.
   * 
   * @param playerId 玩家id.
   * @return .
   */
  @Override
  public RoomCardMemInfoDto memInfo(long playerId) {
    if (!players.contains(playerId)) {
      return null;
    }

    PlayerMgr playerMgr = ApplicationContext.getBean(PlayerMgr.class);
    Player player = playerMgr.getPlayer(playerId);
    if (player == null) {
      return null;
    }

    MemInfo memInfo = player.baseMemInfo();
    RoomCardMemInfoDto roomCardMemInfo = RoomCardMemInfoDtoConvert.INSTANCE.bo2dto(memInfo);

    roomCardMemInfo.setHeadUrl(player.getPlayerAvatar());
    roomCardMemInfo.setIp(player.getLoginIp());
    roomCardMemInfo.setCreateTime(player.getRegisterTime().getTime());
    roomCardMemInfo.setCountry(player.getCountry());
    roomCardMemInfo.setRoundCount(player.getRoundCount());

    S seat = seat(playerId);
    if (seat != null) {
      roomCardMemInfo.setTableId(seat.table().getId());
      roomCardMemInfo.setOrder(seat.order);
      roomCardMemInfo.setChips(seat.getTotalChips());
      roomCardMemInfo.setState(seat.getState() != null ? seat.getState().val : 0);
      roomCardMemInfo.setByStander(seat.isByStander() ? 1 : 0);
      roomCardMemInfo.setSysHost(seat.isSysHost() ? 1 : 0);

    }

    return roomCardMemInfo;
  }

  /**
   * 房间中成员的信息 .
   * 
   * @return .
   */
  public List<MemInfo> memInfos() {
    List<MemInfo> memInfos = new ArrayList<>();

    for (Long playerId : players) {
      MemInfo memInfo = memInfo(playerId);
      if (memInfo != null) {
        memInfos.add(memInfo);
      }
    }

    return memInfos;
  }

  public T table() {
    return this.table;
  }

  @Override
  public Collection<? extends AbstractTable> tables() {
    return Arrays.asList(table);
  }

  @SuppressWarnings("unchecked")
  @Override
  public S seat(long playerId) {
    List<? extends AbstractSeat> list = table().seats();
    // list.forEach((item) -> {
    // if (item.getPlayerId() > 0) {
    // System.out.println(item.getOrder() + " XXXXXXXXXX " + JsonUtil.toJson(item));
    // }
    // });
    S result = null;
    if (EmptyUtil.listIsNotEmpty(list)) {
      for (AbstractSeat item : list) {
        if (item.getPlayerId() == playerId) {
          result = (S) item;
          break;
        }
      }
    }
    return result;
  }
}
